package com.insurance.modules.auth.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.insurance.entity.AuthenticationUserEntity;
import com.insurance.entity.PartnerInfoEntity;
import com.insurance.service.AuthenticationUserService;
import com.insurance.service.PartnerInfoService;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.insurance.common.utils.PageUtils;
import com.insurance.common.utils.R;



/**
 * 统一认证用户
 *
 * @date 2021-03-03 13:40:37
 */
@RestController
@RequestMapping("authentication/authenticationuser")
public class AuthenticationUserController {

    @Autowired
    private AuthenticationUserService authenticationUserService;

    @Autowired
    private PartnerInfoService partnerInfoService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    @RequiresPermissions("authentication:authenticationuser:list")
    public R list(@RequestParam Map<String, Object> params, AuthenticationUserEntity authenticationUserEntity){
        QueryWrapper<AuthenticationUserEntity> queryWrapper = authenticationUserService.getQueryWrapper(authenticationUserEntity);
        PageUtils page = authenticationUserService.queryPage(params, queryWrapper);
        List<AuthenticationUserEntity> dataList = (List<AuthenticationUserEntity>) page.getList();
        if(CollectionUtils.isNotEmpty(dataList)){
            for (AuthenticationUserEntity aue : dataList){
                if(aue != null && StringUtils.isNotBlank(aue.getAppId())){
                    PartnerInfoEntity partnerInfoEntity = partnerInfoService.getByAppId(aue.getAppId());
                    if(partnerInfoEntity != null){
                        aue.setAppIdText(partnerInfoEntity.getPartnerName());
                    }
                }
            }
        }
        return R.ok().put("page", page);
    }

    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    @RequiresPermissions("authentication:authenticationuser:info")
    public R info(@PathVariable("id") String id){
		AuthenticationUserEntity authenticationUser = authenticationUserService.getById(id);
		if(authenticationUser != null && StringUtils.isNotBlank(authenticationUser.getAppId())){
            PartnerInfoEntity partnerInfoEntity = partnerInfoService.getByAppId(authenticationUser.getAppId());
            if(partnerInfoEntity != null){
                authenticationUser.setAppIdText(partnerInfoEntity.getPartnerName());
            }
        }
        return R.ok().put("authenticationUser", authenticationUser);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    @RequiresPermissions("authentication:authenticationuser:save")
    public R save(@RequestBody AuthenticationUserEntity authenticationUser){
		authenticationUserService.save(authenticationUser);
        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("authentication:authenticationuser:update")
    public R update(@RequestBody AuthenticationUserEntity authenticationUser){
		authenticationUserService.updateById(authenticationUser);
        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @RequiresPermissions("authentication:authenticationuser:delete")
    public R delete(@RequestBody String[] ids){
		authenticationUserService.removeByIds(Arrays.asList(ids));
        return R.ok();
    }

}
